/**
 * 转换配置区间
 * @param ranges 区间数组
 * @param outRange 超出区间
 * @param ratio 系数
 */
export function checkAndTransferConfig(
  ranges: {
    start: number;
    end: number;
  }[],
  ratio = 1,
) {
  if (!ranges || !ranges.length)
    return {
      valid: true,
      message: 'success',
      data: [{ start: 0 }],
    };
  const len = ranges.length;
  const res: { start: number; end?: number }[] = [];
  const item = ranges[0];
  if (item.start != 0) res.push({ start: 0, end: item.start * ratio });
  res.push({ start: item.start * ratio, end: item.end * ratio });

  for (let i = 1; i < len; i++) {
    const { start: preStart, end: preEnd } = ranges[i - 1];
    const { start, end } = ranges[i];
    if (preStart === undefined || start === undefined || preEnd === undefined || end === undefined)
      return { valid: false, message: '区间配置错误', data: [] };
    if (preStart >= preEnd)
      return {
        valid: false,
        message: '区间起始值必须小于结束值',
        data: [],
      };
    if (start != preEnd) {
      return {
        valid: false,
        message: '下一区间起始值必须等于上一区间结束值',
        data: [],
      };
    }
    res.push({ start: start * ratio, end: end * ratio });
  }
  res.push({ start: ranges[len - 1].end * ratio });
  return { valid: true, message: 'success', data: res };
}
